package com.orangelife.mobile.goodsrent.activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.goodsrent.adapter.RentAdapter;
import com.orangelife.mobile.goodsrent.biz.GoodsInfoCallBack;
import com.orangelife.mobile.goodsrent.biz.GoodsListImpl;
import com.orangelife.mobile.goodsrent.entity.GoodsInfo;
import com.orangelife.mobile.util.BLog;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;

public class RentListActivity extends GoodsListImpl{
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.lvGoods)
    PullToRefreshListView lvGoods;
    @Bind(R.id.ibShoppingCard)
    ImageButton ibShoppingCard;
    @Bind(R.id.tvTotalPrice)
    TextView tvTotalPrice;
    @Bind(R.id.rlPay)
    RelativeLayout relativeLayout;
    @Bind(R.id.btnParticipate)
    Button btnParticipate;
    @Bind(R.id.etSearchGoods)
    EditText etSearchGoods;
    RentAdapter mAdapter;
    private List<List<GoodsInfo>> mRefreshList;
    private GoodsInfoCallBack mGoodsInfoCallBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rent_list);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setTitleText(tvTitle, getString(R.string.rent));
        setListViewAdapter();
        initShoppingCardNum(ibShoppingCard);
        initGoodsTotalPrice(tvTotalPrice);
        pullListView(lvGoods,mAdapter,etSearchGoods, mRefreshList);
        getShopList(Constant.PAGE_NO_DEFAULT,pageSize);
        onShoppingCardClick();
        onEditTextChangeClick(etSearchGoods);
        BLog.getInstance().intAppmod(BLog.RENT);
    }

    private void onShoppingCardClick() {
        ibShoppingCard.setOnClickListener(goodsListener);
        btnParticipate.setOnClickListener(goodsListener);
    }

    private void getDataFromServer() {
        dialog = DialogHelper.getInstance().createLoadingDialog(this,
                Constant.LOADING);
        dialog.show();
    }

    /***
     * listView设置adapter
     */
    private void setListViewAdapter() {
        getDataFromServer();
        mRefreshList = new ArrayList<>();
        mGoodsInfoCallBack = this;
        mAdapter = new RentAdapter(this,mRefreshList,mGoodsInfoCallBack);
        lvGoods.setPullLoadEnable(false);
        lvGoods.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BLog.getInstance().outAppmod(BLog.RENT);
    }

    @Override
    public void startShopCardAnim(View view) {
        startShopCardAnim(view, ibShoppingCard);
    }

    /**
     * 回调显示物品数量
     */
    @Override
    public void getGoodsRentNum(int num) {
        setShoppingCardNum(ibShoppingCard, num);
    }

    @Override
    public void getGoodsTotalPrice(String totalPrice) {
        setGoodsTotalPrice(tvTotalPrice, totalPrice);
    }

    View.OnClickListener goodsListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.ibShoppingCard:
                    showPopupWindow(relativeLayout);
                    break;
                case R.id.btnParticipate:
                    jumpNewActivity();
                    break;
            }
        }
    };

    @Override
    public void refresh() {
        request(true,lvGoods,mAdapter);
    }

    @Override
    public void loadMore() {
        request(false,lvGoods,mAdapter);
    }
}
